import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;

public class Data extends JPanel
{
    public Data()
    {
        super(new BorderLayout());
        initFields();
        pd = PersonalData.getPersonalData();
        //dateField.setValue(new Date());
        setValues();
        JPanel panel_aux = new JPanel();
        panel_aux.add(getPersonalDataPanel());
        add(panel_aux,BorderLayout.NORTH);
        add(getConfirmPanel(),BorderLayout.CENTER);
    }
    public Data(PersonalData pd_)
    {
        super(new BorderLayout());
        initFields();
        pd = pd_;
        setValues();
        JPanel panel_aux = new JPanel();
        panel_aux.add(getPersonalDataPanel());
        add(panel_aux,BorderLayout.NORTH);
        add(getConfirmPanel(),BorderLayout.CENTER);
    }
    public void initFields()
    {
    	isSaved = false;
        int widthField = 10;
        nameField = Components.getTextField(widthField);
        lastNameField = Components.getTextField(widthField);
        dateBirthField = Components.getFormattedTextField(
        		DateFormat.getDateInstance(DateFormat.SHORT, new Locale("es","MX")));
        maleButton = new JRadioButton(
            Resources.getString(PersonalData.MALE),false);
        maleButton.setFont(
            Resources.getFont(Resources.FONT_TYPE.LABELS));
        femaleButton = new JRadioButton(
            Resources.getString(PersonalData.FEMALE),false);
        femaleButton.setFont(
            Resources.getFont(Resources.FONT_TYPE.FIELDS));
        ButtonGroup group = new ButtonGroup();
        group.add(maleButton);
        group.add(femaleButton);
        occupationField = Components.getTextField(widthField);
        coachsNameField = Components.getTextField(widthField);
        ArrayList <String> sportsName = SomatotypeIO.getSportsList();
        sportsName.add(0,"");
        sportCombo = Components.getComboBox(sportsName.toArray());
        categoryField = Components.getTextField(widthField);
        institutionField = Components.getTextField(widthField);
    }
    public void setValues()
    {
        nameField.setText(pd.getName());
        lastNameField.setText(pd.getLastName());
        if(PersonalData.MALE.equals(pd.getGender()))
            maleButton.setSelected(true);
        else if(PersonalData.FEMALE.equals(pd.getGender()))
            femaleButton.setSelected(true);
        occupationField.setText(pd.getOccupation());
        coachsNameField.setText(pd.getCoachsName());
        sportCombo.setSelectedItem(pd.getSport());
        categoryField.setText(pd.getCategory());
        institutionField.setText(pd.getInstitution());
        dateBirthField.setValue(pd.getDateBirth());
    }
    public PersonalData getData()
    {
        return pd;
    }
    private JPanel getPersonalDataPanel()
    {
        JPanel panelPD = new JPanel(new BorderLayout());
        Border etched = BorderFactory.createEmptyBorder();
        Border titled = BorderFactory.createTitledBorder(
            etched, 
            Resources.getString("personalData"),
            TitledBorder.LEFT, 
            TitledBorder.TOP, 
            Resources.getFont(Resources.FONT_TYPE.BORDER)
        );
        panelPD.setBorder(titled);
        JLabel nameLabel = Components.getLabel(Resources.getString("name"));
        JLabel lastNameLabel = Components.getLabel(Resources.getString("lastName"));
        JLabel dateBirthLabel = Components.getLabel(Resources.getString("dateBirth"));
        JButton calendarButton = new JButton("...");
        calendarButton.setFont(Resources.getFont(Resources.FONT_TYPE.LABELS));
        calendarButton.addActionListener(
    		new ActionListener()
    		{
				public void actionPerformed(ActionEvent e) 
				{
					CalendarDialog calendarD = new CalendarDialog((JFrame)SwingUtilities.getWindowAncestor(Data.this),(Date)dateBirthField.getValue());
					calendarD.setLocation(Elite.getCenteringDim(calendarD.getSize()));
					calendarD.setVisible(true);
					Date[] day = calendarD.getSelectedDays();
					if(calendarD.existSelectedDate())
						dateBirthField.setValue(day[0]);
				}
    		}
		);
        JLabel genderLabel = Components.getLabel(Resources.getString("gender"));
        JLabel occupationLabel = Components.getLabel(Resources.getString("occupation"));
        JLabel coachsNameLabel = Components.getLabel(Resources.getString("coachsName"));
        JLabel sportLabel = Components.getLabel(Resources.getString("sport"));
        JLabel categoryLabel = Components.getLabel(Resources.getString("category"));
        JLabel institutionLabel = Components.getLabel(Resources.getString("institution"));
        JPanel panel_aux1 = new JPanel();//new GridLayout(1,2,15,0));
        JPanel panel_aux2 = new JPanel(new GridLayout(9,2,15,0));
        JPanel panel_aux3 = new JPanel(new BorderLayout());
        //JPanel panel_aux4 = new JPanel(new BorderLayout());
        
        panel_aux2.add(nameLabel);
        panel_aux2.add(nameField);
        panel_aux2.add(lastNameLabel);
        panel_aux2.add(lastNameField);
        panel_aux2.add(dateBirthLabel);
        panel_aux3.add(dateBirthField, BorderLayout.CENTER);
        panel_aux3.add(calendarButton, BorderLayout.EAST);
        panel_aux2.add(panel_aux3);
        panel_aux2.add(genderLabel);
        JPanel panel_aux5 = new JPanel(new GridLayout(1,2));
        panel_aux5.add(maleButton);
        panel_aux5.add(femaleButton);
        panel_aux2.add(panel_aux5);
        panel_aux2.add(occupationLabel);
        panel_aux2.add(occupationField);
        panel_aux2.add(coachsNameLabel);
        panel_aux2.add(coachsNameField);
        panel_aux2.add(sportLabel);
        panel_aux2.add(sportCombo);
        panel_aux2.add(categoryLabel);
        panel_aux2.add(categoryField);
        panel_aux2.add(institutionLabel);
        panel_aux2.add(institutionField);
        panel_aux1.add(panel_aux2);
        //panel_aux1.add(panel_aux4);
        panelPD.add(panel_aux1,BorderLayout.WEST);
        return panelPD;
    }
    private JPanel getConfirmPanel()
    {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER,30,15));
        JButton okButton = new JButton(Resources.getString("ok"));
        okButton.setFont(Resources.getFont(Resources.FONT_TYPE.LABELS));
        okButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                	confirmNewValues();
                	isSaved = false;
                }
            }
        );
        JButton cancelButton = new JButton(Resources.getString("cancel"));
        cancelButton.setFont(Resources.getFont(Resources.FONT_TYPE.LABELS));
        cancelButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    setValues();
                }
            }
        );
        JPanel panel_aux1 = new JPanel(new GridLayout(1,2,30,0));
        panel_aux1.add(okButton);
        panel_aux1.add(cancelButton);
        panel.add(panel_aux1);
        return panel;
    }
    public void confirmNewValues()
    {
    	pd.setName(nameField.getText());
        pd.setLastName(lastNameField.getText());
        if(((Date)dateBirthField.getValue()).compareTo(new Date()) < 0)
        	pd.setDateBirth((Date)dateBirthField.getValue());
        else
        	dateBirthField.setValue(pd.getDateBirth());
        if(maleButton.isSelected())
            pd.setGender(PersonalData.MALE);
        else
            pd.setGender(PersonalData.FEMALE);
        pd.setOccupation(occupationField.getText());
        pd.setCoachsName(coachsNameField.getText());
        pd.setSport(sportCombo.getSelectedItem().toString());
        pd.setCategory(categoryField.getText());
        pd.setInstitution(institutionField.getText());
    }
    public boolean existNewInfo()
    {
    	if(!pd.getName().equals(nameField.getText()))
    		return true;
    	if(!pd.getLastName().equals(lastNameField.getText()))
    		return true;
    	if(maleButton.isSelected())
    	{
    		if(!pd.getGender().equals(PersonalData.MALE))
    			return true;
    	}
		else
			if(!pd.getGender().equals(PersonalData.FEMALE))
    			return true;
    	if(!pd.getOccupation().equals(occupationField.getText()))
    		return true;
    	if(!pd.getCoachsName().equals(coachsNameField.getText()))
    		return true;
    	if(!pd.getSport().equals((String)sportCombo.getSelectedItem()))
    		return true;
    	if(!pd.getCategory().equals(categoryField.getText()))
    		return true;
    	if(!pd.getInstitution().equals(institutionField.getText()))
    		return true;
    	if(!Resources.getDateFormat(pd.getDateBirth()).equals(dateBirthField.getText()))
    		return true;
    	return false;
    }
    public void setSavedState(boolean isSaved_)
    {
    	isSaved = isSaved_;
    }
    public boolean getSavedState()
    {
    	return isSaved;
    }
    private boolean isSaved;
    private PersonalData pd;
    private JTextField nameField;
    private JTextField lastNameField;
    private JFormattedTextField dateBirthField;//date
    private JRadioButton maleButton;
    private JRadioButton femaleButton;
    private JTextField occupationField;
    private JTextField coachsNameField;
    private JComboBox sportCombo;
    private JTextField categoryField;
    private JTextField institutionField;
}